$(document).ready(function() {
	moment.locale("zh-cn");
	window.currentpage = 1;
	ready();
	
});

function ready() {
	queryUserInfo(1);//查询第一页
	//绑定事件
	bind();
	//表单校验
	check();
	showUserName();
}

function bind() {
	$("#pageSize").bind("change",function(){
		queryUserInfo(window.currentpage);
	});
	$("#stuStatus").bind("change",function(){
		queryUserInfo(window.currentpage);
	});
	$("#searchStuByStuid").bind("click",function(){
		window.currentpage = 1;
		queryUserInfo(window.currentpage);
	});
	$('#addStuInfoModal').on('hide.bs.modal', function (){
		$("#insertStuId").val("");
		$("#insertStuGrade").val("");
		$("#insertStuName").val("");
		$("#insertStuSex").val("");
		$("#insertStuId-error").remove();
		$("#insertStuGrade-error").remove();
		$("#insertStuName-error").remove();
		$("#insertStuSex-error").remove();
	});
	$('#updateStuInfoModal').on('hide.bs.modal', function (){
		$("#updateStuId").val("");
		$("#updateStuGrade").val("");
		$("#updateStuName").val("");
		$("#updateStuSex").val("");
		$("#updateStuId-error").remove();
		$("#updateStuGrade-error").remove();
		$("#updateStuName-error").remove();
		$("#updateStuSex-error").remove();
	});
	$('#updatePasswordModel').on('hide.bs.modal', function (){
		$("#oldPassword").val("");
		$("#password").val("");
		$("#password_again").val("");
		$("#oldPassword-error").remove();
		$("#password-error").remove();
		$("#password_again-error").remove();
	});
	$('input[name="selectAllStu"]').bind("click",function(){
		if($(this).is(':checked')){
			$('input[name="selectStu"]').each(function(){   
                $(this).prop("checked",true);  
            });
		}else{
			$('input[name="selectStu"]').each(function(){   
                $(this).removeAttr("checked",false);
            });
		}
	});
}

function check(){
	addCheck();
	updateCheck();
	updatePasswordCheck();
}


function addCheck(){
	//添加校验
	$("#addStuInfo").validate({
		rules:{
			insertStuId:{
				required:true,
				digits:true,
				minlength:12,
				maxlength:12
			},
			insertStuName:{
				required:true,
			},
			insertStuGrade:{
				required:true,
				digits:true,
				minlength:4,
				maxlength:4
			},
			insertStuSex:{
				required:true
			}
		},
		messages:{
			insertStuId:{
				required:"请输入学号",
				digits:"请输入正确格式的学号",
				minlength:"请输入正确格式的学号",
				maxlength:"请输入正确格式的学号"
			},
			insertStuName:{
				required:"请输入姓名",
			},
			insertStuId:{
				required:"请输入学号",
				digits:"请输入正确格式的学号",
				minlength:"请输入正确格式的学号",
				maxlength:"请输入正确格式的学号"
			},
			insertStuSex:{
				required:"请选择性别"
			}
		},
		submitHandler:function(form){
			var stuId = $("#insertStuId").val();
			var stuGrade = $("#insertStuGrade").val();
			var stuName = $("#insertStuName").val();
			var stuSex = $("#insertStuSex").val();
			$.ajax({
				type:'post',
				url:'insertStuInfo',
				data:{"stuId":stuId,"stuGrade":stuGrade,"stuName":stuName,"stuSex":stuSex},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#addStuInfoModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function updateCheck(){
	//修改校验
	$("#updateStuInfo").validate({
		rules:{
			updateStuId:{
				required:true,
				digits:true,
				minlength:12,
				maxlength:12
			},
			updateStuName:{
				required:true,
			},
			updateStuGrade:{
				required:true,
				digits:true,
				minlength:4,
				maxlength:4
			},
			updateStuSex:{
				required:true
			}
		},
		messages:{
			updateStuId:{
				required:"请输入学号",
				digits:"请输入正确格式的学号",
				minlength:"请输入正确格式的学号",
				maxlength:"请输入正确格式的学号"
			},
			updateStuName:{
				required:"请输入姓名",
			},
			updateStuId:{
				required:"请输入学号",
				digits:"请输入正确格式的学号",
				minlength:"请输入正确格式的学号",
				maxlength:"请输入正确格式的学号"
			},
			updateStuSex:{
				required:"请选择性别"
			}
		},
		submitHandler:function(form){
			var stuId = $("#updateStuId").val();
			var stuGrade = $("#updateStuGrade").val();
			var stuName = $("#updateStuName").val();
			var stuSex = $("#updateStuSex").val();
			$.ajax({
				type:'post',
				url:'updateStuInfoBySuper',
				data:{"stuId":stuId,"stuGrade":stuGrade,"stuName":stuName,"stuSex":stuSex},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updateStuInfoModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function updatePasswordCheck(){
	$("#updatePassword").validate({
		rules:{
			oldPassword:{
				required:true
			},
			password:{
				required:true
			},
			password_again:{
				required:true,
				equalTo: "#password"
			}
		},
		messages:{
			oldPassword:{
				required:"请输入旧密码"
			},
			password:{
				required:"请输入新密码"
			},
			password_again:{
				required:"请输入新密码",
				equalTo: "两次输入不相同"
			}
		},
		submitHandler:function(form){
			var oldPassword = $("#oldPassword").val();
			var password = $("#password").val();
			$.ajax({
				type:'post',
				url:'updatePassword',
				data:{"oldPassword":oldPassword,"password":password},
				success:function(data){
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updatePasswordModel').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function queryUserInfo(page){
	var currPage = page;
	var pageSize = $("#pageSize").val();
	var stuStatus = $("#stuStatus").val();
	var stuId = $("#stuId").val();
	$.ajax({
		url:"/dormms/super/queryStuInfoByPage",
        type:"post",
        data:{"currPage":currPage,"pageSize":pageSize,"stuId":stuId,"isLeave":stuStatus},
        success:function(data){
        	console.log(data);
        	$("#stuInfoList").empty();
        	$("#total").text("共有"+data.total+"条记录");
        	for(var i=0; i<data.list.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
        		 trTemp.append("<td id='findStuId'>"+ data.list[i].stuId +"</td>");
        		 trTemp.append("<td>"+ data.list[i].stuName +"</td>");
        		 trTemp.append("<td>"+ data.list[i].stuGrade +"</td>");
        		 if(data.list[i].stuTel != null){
        			 trTemp.append("<td>"+ data.list[i].stuTel +"</td>");
        		 }
        		 else{
        			 trTemp.append("<td></td>");
        		 }
        		 trTemp.append("<td>" + "<button type='button' class='btn btn-outline-success btn-sm' onclick='getStuInfoGet(this)'>查看</button>" 
        				 +"<button type='button' class='btn btn-outline-primary btn-sm' onclick='getStuInfoUpdate(this)'>修改</button>" 
        				 + "<button type='button' class='btn btn-outline-danger btn-sm' onclick='deleteStuInfo(this)'>离校</button>" 
        				 + "</td>");
        		 trTemp.appendTo("#stuInfoList");
        	}
        	queryUserRoleByPage(data);
        }
	});
}

function queryUserRoleByPage(data) {
	var options = {
			bootstrapMajorVersion: 3,
			currentPage: data.pageNum,
            totalPages: data.pages,
            numberofPages: 5,
            itemTexts: function(type, page, current) { //修改显示文字
                switch (type) {
                case "first":
                    return "&laquo;";
                case "prev":
                    return "&lt;";
                case "next":
                    return "&gt;";
                case "last":
                    return "&raquo;";
                case "page":
                    return page;
                }
            },
            onPageClicked: function (event, originalEvent, type, page) { //异步换页
            	queryUserInfo(page);
            	window.currentpage=page;
            },
	};
	$('#page').bootstrapPaginator(options); 
}


//获取当前行数据(修改)
function getStuInfoUpdate(obj) {
	var x = $(obj).parent().parent().find("td");
	var stuinfo = x.eq(0).text();
	$.ajax({
		url:"/dormms/super/queryStuInfoByStuId",
		type:"get",
		data:{'stuId':stuinfo},
		success:function(data){
			console.log(data);
			$("#updateStuId").val(data.stuId);
			$("#updateStuId").attr("readonly","readonly");
			$("#updateStuName").val(data.stuName);
			$("#updateStuSex").val(data.stuSex);
			$("#updateStuGrade").val(data.stuGrade);
			$('#updateStuInfoModal').modal('show');
		}
	});
}
//获取当前行数据(查看)
function getStuInfoGet(obj) {
	var x = $(obj).parent().parent().find("td");
	var stuinfo = x.eq(0).text();
	$.ajax({
		url:"/dormms/super/queryStuInfoByStuId",
		type:"get",
		data:{'stuId':stuinfo},
		success:function(data){
			console.log(data);
			$("#getStuId").val(data.stuId);
			$("#getStuName").val(data.stuName);
			$("#getStuSex").val(data.stuSex);
			$("#getStuGrade").val(data.stuGrade);
			console.log(data.stuBirthday);
			if(data.stuBirthday!=null){
				$("#getStuBirthday").val(moment(data.stuBirthday).format('L'));
			}else{
				$("#getStuBirthday").val("");
			}
			$("#getStuTel").val(data.stuTel);
			$("#getStuParentTel").val(data.stuParentTel);
			$('#getStuInfoModal').modal('show');
		}
	});
}

function deleteStuInfo(obj){
	var x = $(obj).parent().parent().find("td");
	var stuinfo = x.eq(0).text();
	$.ajax({
		url:"deleteStuInfoByStuId",
		type:"get",
		data:{'stuId':stuinfo},
		success:function(data){
			queryUserInfo(window.currentpage);
		}
	});
}

function showPasswordModel() {
	$('#updatePasswordModel').modal('show');
}

function showUserName() {
	$.ajax({
		url:"getSuperInfo",
		type:"get",
		success:function(data){
			$("#userId").text(data.userId);
		}
	});
}
